package com.example.asuper.check;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asuper.check.Db.CheckDAL;
import com.example.asuper.check.Db.CheckDbService;
import com.example.asuper.check.Db.DbOpenHelper;
import com.example.asuper.check.Model.CheckModel;

import java.util.ArrayList;

public class AddActivity extends Activity implements View.OnClickListener {
    TextView mTv_showSingle;
    EditText mEdit_addCode;
    EditText mEdit_addName;
    EditText mEdit_addMsg;
    EditText mEdit_addState;
    Button mBtn_add;
    Button mBtn_del;
    Button mBtn_update;
    Button mBtn_selectSingle;
    Button mBtn_selectAll;
    Button mBtn_create;
    ListView mLv_showAll;
    private static String TAG = "TESTDB";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        ;
        mTv_showSingle = (TextView) findViewById(R.id.tv_add_showsingle);
        mEdit_addCode = (EditText) findViewById(R.id.edit_addcode);
        mEdit_addName = (EditText) findViewById(R.id.edit_addname);
        mEdit_addMsg = (EditText) findViewById(R.id.edit_addmsg);
        mEdit_addState = (EditText) findViewById(R.id.edit_addstate);
        mBtn_add = (Button) findViewById(R.id.btn_add_add);
        mBtn_del = (Button) findViewById(R.id.btn_add_delete);
        mBtn_update = (Button) findViewById(R.id.btn_add_update);
        mBtn_selectSingle = (Button) findViewById(R.id.btn_add_selectsingle);
        mBtn_selectAll = (Button) findViewById(R.id.btn_add_selectall);
        mBtn_create = (Button) findViewById(R.id.btn_add_create);
        mBtn_add.setOnClickListener(this);
        mBtn_del.setOnClickListener(this);
        mBtn_update.setOnClickListener(this);
        mBtn_selectSingle.setOnClickListener(this);
        mBtn_selectAll.setOnClickListener(this);
        mBtn_create.setOnClickListener(this);
        mLv_showAll=(ListView)findViewById(R.id.lv_add_showlist);
    }

    @Override
    public void onClick(View v) {
        DbOpenHelper helper = null;
        CheckDbService checkDbService = null;
        CheckModel checkModel = null;
        switch (v.getId()) {
            case R.id.btn_add_create:
                helper = new DbOpenHelper(this);
                helper.getReadableDatabase();
                Log.i(TAG, "CREATE=====>>>创建数据库");
                Toast.makeText(this, "创建数据库", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_add_add:
                checkDbService = new CheckDAL(this);
                checkModel = new CheckModel();
                checkModel.setCheckName(mEdit_addName.getText().toString());
                checkModel.setCheckCode(mEdit_addCode.getText().toString());
                checkModel.setRemark(mEdit_addMsg.getText().toString());
                checkModel.setState(mEdit_addState.getText().toString());
                Log.i(TAG, "ADD======>>>" + checkModel.toString());
                boolean addResult = checkDbService.addCheckData(checkModel);
                Toast.makeText(this, "=====>>>" + addResult, Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_add_delete:
                checkDbService = new CheckDAL(this);
                Log.i(TAG, "DELETE======>>>" + mEdit_addCode.getText().toString());
                boolean delResult = checkDbService.deleteCheckData(mEdit_addCode.getText().toString(), "1");
                Toast.makeText(this, "=====>>>" + delResult, Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_add_update:
                checkDbService = new CheckDAL(this);
                Log.i(TAG, "UPDATE======>>>" + mEdit_addCode.getText().toString());
                boolean updateResult = checkDbService.updateCheckData(mEdit_addCode.getText().toString(), mEdit_addState.getText().toString());
                Toast.makeText(this, "=====>>>" + updateResult, Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_add_selectsingle:
                checkDbService = new CheckDAL(this);
                Log.i(TAG, "GET1======>>>" + mEdit_addCode.getText().toString());
                try {
                    CheckModel getChecksigle = checkDbService.getCheckModel(mEdit_addCode.getText().toString());
                    mTv_showSingle.setText(getChecksigle.toString());
                    Toast.makeText(this, "=====>>>" + getChecksigle.toString(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(this,"=====>>"+"无数据",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_add_selectall:
                checkDbService = new CheckDAL(this);
                Log.i(TAG, "GET2======>>>" + mEdit_addState.getText().toString());
                try {
                    ArrayList<CheckModel> checkModelArrayList = checkDbService.getCheckList(mEdit_addState.getText().toString());
                    ArrayAdapter adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,checkModelArrayList);
                    mLv_showAll.setAdapter(adapter);
                }catch (Exception e){
                    Toast.makeText(this,"=====>>"+"无数据",Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }
}
